<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
include_once( "class_6h/AutoRateConfig_6h.php" );
$gametype = 0;
$tblName = $g_gametype_3d[$gametype]['ch'];
$tbl = $g_gametype_3d[$gametype]['en'];
$ui = new UI();
$conf = new Config();
$tpl = new Template2();
$AutoRateConfig_6h = new AutoRateConfig_6h($g_betname, $g_autorate, $gametype);

$byPass_Arr = array(24, 26, 31, 37, 38);
if (ispost())
{
    $fields_data = array();
    foreach ($g_betname as $key => $val)
    {
        if (in_array($key, $g_autorate) && !in_array($key, $byPass_Arr))
        {
            $money = "money_" . $key;
            $downrate = "downrate_" . $key;
            $uprate = "uprate_" . $key;
            $limitrate = "limitrate" . $key;
            $typeflag = "typeflag" . $key;
            $amount_255 = "amount_255" . $key;
            $money1 = "money1_" . $key;
            $downrate1 = "downrate1_" . $key;
            $uprate1 = "uprate1_" . $key;
            if (isset($_POST[$uprate]))
            {
                $uprate_val = floatval(trim($_POST[$uprate]));
            }
            else
            {
                $uprate_val = 0;
            }
            if (isset($_POST[$uprate1]))
            {
                $uprate1_val = floatval(trim($_POST[$uprate1]));
            }
            else
            {
                $uprate1_val = 0;
            }
            if (isset($_POST[$amount_255]))
            {
                $amount_255_val = intval(trim($_POST[$amount_255]));
            }
            else
            {
                $amount_255_val = 0;
            }
            if (isset($_POST[$downrate]))
            {
                $downrate_val = floatval(trim($_POST[$downrate]));
            }
            else
            {
                $downrate_val = 0;
            }
            if (isset($_POST[$downrate1]))
            {
                $downrate1_val = floatval(trim($_POST[$downrate1]));
            }
            else
            {
                $downrate1_val = 0;
            }
            if (isset($_POST[$limitrate]))
            {
                $limitrate_val = floatval(trim($_POST[$limitrate]));
            }
            else
            {
                $limitrate_val = 0;
            }
            if (isset($_POST[$money]))
            {
                $money_val = intval(trim($_POST[$money]));
            }
            else
            {
                $money_val = 0;
            }
            if (isset($_POST[$money1]))
            {
                $money1_val = intval(trim($_POST[$money1]));
            }
            else
            {
                $money1_val = 0;
            }
            if (in_array($key, array(1, 2, 3, 5, 6, 15, 16, 17, 20, 23, 28, 30, 35, 47)))
            {
                $typeflag_val = 1;
            }
            else
            {
                $typeflag_val = 0;
            }
            $fields_data[] = array("id" => $key, "subid" => 0, "money" => $money_val, "money1" => $money1_val, "downrate" => $downrate_val, "downrate1" => $downrate1_val, "uprate" => $uprate_val, "uprate1" => $uprate1_val, "limitrate" => $limitrate_val, "typeflag" => $typeflag_val, "amount_255" => $amount_255_val);
            if ($money_val == 0)
            {
                getdb("update autorate_config_ss set money = 0, updatetime = '" . date("Y-m-d H:i:s") . "' where bettype = {$key} and typeflag = {$typeflag_val} and money != 0");
            }
        }
    }
    $AutoRateConfig_6h->updateAutoRate($fields_data);
    unset($fields_data);
}
$limit = array();
foreach ($g_autorate as $key)
{
    $limit[$key] = 0;
}
$autorate_mode = 0;
if (29 <= count($g_betname) && $g_betname[29] == "五不中")
{
    $autorate_mode = 1;
}
if (ispost())
{
    $all_ok = 0;
    if ($autorate_mode == 1)
    {
        if (isset($_POST['auto_down']))
        {
            $adown = floatval($_POST['auto_down']);
        }
        else
        {
            $adown = 0;
        }
        if ($adown < 0)
        {
            $adown = $adown * ( 0 - 1 );
        }
        $i = 0;
        while ($i < 10)
        {
            $ok = 0;
            $auto_num = array("", "", "", "", "");
            $j = 0;
            while ($j < 5)
            {
                if (isset($_POST["auto_n" . $i . "_" . $j]))
                {
                    $nn = intval($_POST["auto_n" . $i . "_" . $j]);
                }
                else
                {
                    $nn = 0;
                }
                if ($nn != 0)
                {
                    $auto_num[$j] = d2($nn);
                    ++$ok;
                }
                ++$j;
            }
            if ($ok == 5)
            {
                sort($auto_num);
                $conf->set("autorate_b29_num" . $i, implode(",", $auto_num));
                ++$all_ok;
            }
            else
            {
                $conf->set("autorate_b29_num" . $i, "");
            }
            ++$i;
        }
        if (0 < $all_ok)
        {
            $conf->set("autorate_b29_down", $adown);
        }
        else
        {
            $conf->set("autorate_b29_down", "");
        }
    }
}
$theadContent = "<tr class=\"td0\"><td rowspan=2>玩法</td><td colspan=3>第一阶段(一次性)</td><td colspan=3>第二阶段(累进)</td><td rowspan=2>最低赔率</td></tr>";
$theadContent .= "<td class=\"td1\">金额</td><td class=\"td1\">降赔率</td><td class=\"td1\">升赔率</td><td class=\"td1\">金额</td><td class=\"td1\">降赔率</td><td class=\"td1\">升赔率</td>";
$autorateData = $AutoRateConfig_6h->getAutoRateAll();
$tempStr = "";
$tempI = 0;
$tempClass = 2;
$showHeadArr = array();
if ($autorateData == false)
{
    $cc = 0;
    foreach ($g_betname as $key => $val)
    {
        if (!in_array($key, $g_autorate))
        {
            continue;
        }
        if (in_array($key, $byPass_Arr))
        {
            continue;
        }
        if (in_array($key, array(1, 2, 3, 5, 6, 15, 16, 17, 20, 23, 28, 30, 35)))
        {
            $typeflag = 1;
            $up = $conf->get("autorate_up" . $key);
        }
        else
        {
            $typeflag = 0;
            $up = 0;
        }
        $amoney = $conf->get("autorate_money" . $key);
        $adown = $conf->get("autorate_down" . $key);
        $fields_data = array("bettype_id" => $key, "subid" => 0, "money" => $amoney, "money1" => $amoney, "downrate" => $adown, "downrate1" => $adown, "uprate" => $up, "uprate1" => $up, "limitrate" => $conf->get("autorate_limit" . $key), "typeflag" => $typeflag, "amount_255" => 0);
        $ret = db_insert("autorate_config_6h", $fields_data);
        if ($ret)
        {
            ++$cc;
        }
    }
    $rs = getdb("!!SELECT COUNT(*) FROM autorate_config_6h");
    if ($rs->f[0] == $cc)
    {
        getdb("DELETE FROM k_config WHERE id LIKE 'autorate_down%' ");
        getdb("DELETE FROM k_config WHERE id LIKE 'autorate_limit%' ");
        getdb("DELETE FROM k_config WHERE id LIKE 'autorate_up%' ");
        getdb("DELETE FROM k_config WHERE id LIKE 'autorate_money%' ");
    }
}
else
{
    foreach ($g_betname as $key => $valName)
    {
        if (!in_array($key, $g_autorate))
        {
            continue;
        }
        if (isset($showHeadArr[$key]))
        {
            $tempStr .= $theadContent;
        }
        if (in_array($key, $byPass_Arr))
        {
            $tempStr .= "<tr class=\"td2 row{$tempClass}\"><th>{$valName}</th><td colspan=7 align=ccenter>B类的玩法同A类设定共用</td></tr>";
        }
        else if (isset($autorateData[$key]))
        {
            $val = $autorateData[$key];
            ++$tempI;
            $tempClass = $tempClass % 2 + 1;
            $tempStr .= "<tr class=\"td2 row{$tempClass}\">";
            $tempStr .= "<td class=\"td17\" align=left>{$valName}<input type=\"hidden\" value=\"" . $val['typeflag'] . "\" name=\"typeflag" . $val['bettype_id'] . "\"  /></td>";
            $tempStr .= "<td><input type=\"text\" name=\"money1_" . $val['bettype_id'] . "\" value=\"" . $val['money1'] . "\" size=\"8\" /></td>";
            $tempStr .= "<td><input type=\"text\" name=\"downrate1_" . $val['bettype_id'] . "\" value=\"" . $val['downrate1'] . "\" size=\"5\" /></td>";
            $tempStr .= "<td>";
            if ($val['typeflag'] == 1)
            {
                $tempStr .= "<input type=\"text\" name=\"uprate1_" . $val['bettype_id'] . "\" value=\"" . $val['uprate1'] . "\" size=\"5\" />";
            }
            else
            {
                $tempStr .= "&nbsp;";
            }
            $tempStr .= "<td><input type=\"text\" name=\"money_" . $val['bettype_id'] . "\" value=\"" . $val['money'] . "\" size=\"8\" /></td>";
            $tempStr .= "<td><input type=\"text\" name=\"downrate_" . $val['bettype_id'] . "\" value=\"" . $val['downrate'] . "\" size=\"5\" /></td>";
            $tempStr .= "<td>";
            if ($val['typeflag'] == 1)
            {
                $tempStr .= "<input type=\"text\" name=\"uprate_" . $val['bettype_id'] . "\" value=\"" . $val['uprate'] . "\" size=\"5\" />";
            }
            else
            {
                $tempStr .= "&nbsp;";
            }
            $tempStr .= "</td>";
            $tempStr .= "<td><input type=\"text\" name=\"limitrate" . $val['bettype_id'] . "\" value=\"" . $val['limitrate'] . "\" size=\"5\" /></td>";
        }
        else
        {
            $tempStr .= "<tr class=\"td2 row{$tempClass}\"><th>{$valName}</th><td colspan=7 align=ccenter>查无此玩法的相关资料，请洽系统管理者</td></tr>";
        }
    }
    unset($autorateData);
}
$auto_num = array();
$i = 0;
while ($i < 10)
{
    $nn = $conf->get("autorate_b29_num" . $i);
    if (substr_count($nn, ",") == 4)
    {
        $auto_num[$i] = explode(",", $nn);
    }
    else
    {
        $auto_num[$i] = array("", "", "", "", "");
    }
    ++$i;
}
$auto_down = $conf->get("autorate_b29_down");
$b29list = "";
if ($autorate_mode == 1)
{
    $b29list .= "<br>自选不中热门号码组合自动降赔率设定（每一组合必须为5个号码）";
    $b29list .= "<table class=table0 border=0 cellpadding=3 cellspacing=1>";
    $b29list .= "<tr class=td0 align=center><td>组合序<td>号码1<td>号码2<td>号码3<td>号码4<td>号码5";
    $i = 0;
    while ($i < 10)
    {
        $b29list .= "<tr class=td2 valign=top align=center>";
        $b29list .= "<td>" . ( $i + 1 );
        $b29list .= "<td><input name=auto_n" . $i . "_0 size=2 maxlength=2 value='" . $auto_num[$i][0] . "'>";
        $b29list .= "<td><input name=auto_n" . $i . "_1 size=2 maxlength=2 value='" . $auto_num[$i][1] . "'>";
        $b29list .= "<td><input name=auto_n" . $i . "_2 size=2 maxlength=2 value='" . $auto_num[$i][2] . "'>";
        $b29list .= "<td><input name=auto_n" . $i . "_3 size=2 maxlength=2 value='" . $auto_num[$i][3] . "'>";
        $b29list .= "<td><input name=auto_n" . $i . "_4 size=2 maxlength=2 value='" . $auto_num[$i][4] . "'>";
        ++$i;
    }
    $b29list .= "</table><br>降赔率 <input name=auto_down size=4 value=\"" . $auto_down . "\">";
}
$tpl->load("main", "autorate.htm");
$tpl->set(array("action" => "op.php?op=admin&fp=autorate&gametype={$gametype}", "reload" => "op.php?op=admin&fp=autorate&gametype={$gametype}", "tbodycontente" => $tempStr, "tblName" => $tblName, "theadContent" => $theadContent, "b29list" => $b29list));
include( "admin_menu.php" );
echo "<td>";
$tpl->show("main");
?>